package bitmap
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class BitmapLayer extends Bitmap
	{
		private var _renderSmoothing:Boolean;
		private var _bitmapClipLists:Vector.<BitmapClipList>;
		private var _renderRec:Rectangle = new Rectangle();
		private var _renderPoint:Point = new Point();
		private var _renderColorTransfrom:ColorTransform = null;
		private var _renderMatrix:Matrix = new Matrix();
		private var _hasContentToErase:Boolean = false;
		private var _renderCopy:Boolean;
		private var _layerRect:Rectangle;
		
		public function BitmapLayer(width:int, height:int, renderCopy:Boolean=true, renderSmoothing:Boolean = true)
		{
			super(new BitmapData(width, height, true, 0x000000), PixelSnapping.AUTO, false);
			_layerRect = new Rectangle(0,0,width,height);
			_renderCopy=renderCopy;
			_renderSmoothing=renderSmoothing;
			_bitmapClipLists=new Vector.<BitmapClipList>();
		}
		
		/**
		 *在列表末尾追加 
		 */		
		public function append(bitmapClipList:BitmapClipList):void
		{
			_bitmapClipLists.push(bitmapClipList);
		}
		
		/**
		 *在列表开头追加 
		 */	
		public function prepend(bitmapClipList:BitmapClipList):void
		{
			_bitmapClipLists.unshift(bitmapClipList);
		}
		
		/**
		 *渲染所有层 
		 */		
		public function render():void
		{
			if(!stage)return;
			if (_hasContentToErase)
			{
				bitmapData.fillRect(_layerRect, 0x000000);
				_hasContentToErase = false ;
			}
			bitmapData.lock();
			var len:int=_bitmapClipLists.length;
			for (var i:int=0;i<len;i++)
			{
				var _bitmapClipList:BitmapClipList=_bitmapClipLists[i];
				var bcLen:int=_bitmapClipList.bitmapMovieClips.length;
				for(var j:int=0;j<bcLen;j++)
				{
					var _bitmapMovieClip:BitmapMovieClip=_bitmapClipList.bitmapMovieClips[j];
					if (_bitmapMovieClip.visible)
					{
						var frame:BitmapFrame 	= _bitmapMovieClip.currentFrame;
						_renderColorTransfrom = _bitmapMovieClip.colorTransform;
						
						if (_renderCopy && !_renderColorTransfrom && _bitmapMovieClip.rotation == 0)
						{
							_renderRec.width = frame.width;
							_renderRec.height = frame.height;
							_renderPoint.x = _bitmapMovieClip.x - frame.registerX;
							_renderPoint.y = _bitmapMovieClip.y - frame.registerY;
							bitmapData.copyPixels(frame.bitmapData, _renderRec, _renderPoint, null, null, true);
						}
						else
						{
							_renderMatrix.identity();
							_renderMatrix.translate(-frame.registerX, -frame.registerY);
							_renderMatrix.scale(_bitmapMovieClip.scaleX, _bitmapMovieClip.scaleY);
							if(_bitmapMovieClip.rotation!=0)_renderMatrix.rotate(_bitmapMovieClip.rotation/180 * 3.14159); 
							_renderMatrix.translate(_bitmapMovieClip.x, _bitmapMovieClip.y);
							bitmapData.draw(frame.bitmapData, _renderMatrix, _renderColorTransfrom, _bitmapMovieClip.blendMode, null, _renderSmoothing);
						}
						_hasContentToErase = true;
					}
					if (_bitmapMovieClip.isPlaying)
					{
						_bitmapMovieClip.update();
					}
				}
			}
			bitmapData.unlock();
		}
		
		public function dispose():void
		{
			bitmapData.dispose();
			
			var bcl:BitmapClipList;
			while(_bitmapClipLists.length>0)
			{
				bcl=_bitmapClipLists.shift();
				bcl.dispose();
				bcl=null;
			}
			bcl=null;
			_bitmapClipLists = null;
			_renderRec=null;
			_renderPoint=null;
			_renderColorTransfrom=null;
			_renderMatrix=null;
			_layerRect=null;
			if(parent)parent.removeChild(this);
		}
	}
}